TUTORIAL FIVE
Adding A Player

After the world has been loaded, we must create a player for our game. As a first person shooter, the player is essentially the camera. You move the camera and are treated to a view of the world in the first person.



So that we can re-use our player initialisation code, we will place the code somewhere we can trigger it by the simple switching of a variable value. So that the player is initialised the first time we run the game, we will set the trigger variable first:

rem TUT5A
rem Trigger player initialisation
restart=1
We then provide the code that will setup the player when this variable is set to one. By using BSP collision on the camera, we completely automate the handling of collision within the 3D world. This not only provides sliding collision on any surface, but does so at great speed:

rem TUT5B
rem In case of restart
if restart=1
   restart=0
   set bsp collision off 1
   rotate camera 0,0,0
   position camera 2,2,2
   set bsp camera collision 1,0,0.75,0
endif
After the player has been setup, we can start to control it. We will first need to provide control of the players rotation, movement and gravity:

rem TUT5C
rem Control player direction
rotate camera camera angle x(0)+(mousemovey()/2.0),camera angle y(0)+(mousemovex()/2.0),0

rem Control player movement
cx#=camera angle x(0) : cy#=camera angle y(0)
if upkey()=1 then xrotate camera 0,0 : move camera 0,0.2 : xrotate camera 0,cx#
if downkey()=1 then xrotate camera 0,0 : move camera 0,-0.2 : xrotate camera 0,cx#
if leftkey()=1 then yrotate camera 0,cy#-90 : move camera 0.2 : yrotate camera 0,cy#
if rightkey()=1 then yrotate camera 0,cy#+90 : move camera 0.2 : yrotate camera 0,cy#
if wrapvalue(camera angle x(0))>40 and wrapvalue(camera angle x(0))<180 then xrotate camera 0,40
if wrapvalue(camera angle x(0))>180 and wrapvalue(camera angle x(0))<280 then xrotate camera 0,280

rem Apply simple gravity to player
position camera camera position x(),camera position y()-0.1,camera position z()
In addition, we must ensure the player is always at the center of the universe. To that end, when ever the player moves we must adjust the position of the sky and the 3D listener which represents the player in our game:

rem TUT5D
rem Player is always focal point of sky
position object SkyObj,camera position x(),camera position y(),camera position z()

rem Position listener at player for 3D sound
position listener camera position x(),camera position y(),camera position z()
rotate listener camera angle x(),camera angle y(),camera angle z()

CLICK HERE TO RETURN TO THE MAIN MENU